package com.ayuer.chapter05;

import java.util.Arrays;

public class FindKthLargest {

    public static int findKthLargest(int[] nums, int k) {
        MinHeap minHeap = new MinHeap(k);
        for (int i = 0; i < k; i++) {
            minHeap.offer(nums[i]);
        }
        System.out.println(Arrays.toString(minHeap.array));
        for (int i = k; i < nums.length; i++) {
            //判断堆顶元素是否大于
            if (minHeap.peek() < nums[i]) {
                minHeap.replace(nums[i]);
            }
        }
        System.out.println(Arrays.toString(minHeap.array));
        return minHeap.peek();
    }

    public static void main(String[] args) {
        int[] array = new int[]{3, 2, 3, 1, 2, 4, 5, 5, 6};
        System.out.println(findKthLargest(array, 4));
    }
}
